package com.ebiz.mockup.proxies
{
	import com.ebiz.mockup.component.BoardCell;
	import com.ebiz.mockup.component.IBoardJoint;
	import com.ebiz.mockup.core.Core;
	import com.ebiz.mockup.utils.CheckUtils;
	
	import flash.events.Event;
	
	import mx.utils.ArrayUtil;
	
	import org.fas.utils.FuArray;
	import org.puremvc.as3.multicore.patterns.proxy.Proxy;
	
	public class SelectedCelsProxy extends Proxy
	{
		public static const NAME:String = "SelectedCelsProxy";
		
		private static var _instance:SelectedCelsProxy;
		
		public static function get instance():SelectedCelsProxy{
			if(_instance==null){
				_instance = new SelectedCelsProxy();
			}
			return _instance;
		}
		
		public var cells:Array = new Array();
		
		[Bindable]
		public var cellLength:int = 0;
		
		[Bindable]
		public var hasJointInCells:Boolean = false;
		
		public var jointInCells:IBoardJoint = null;
		
		public function SelectedCelsProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		public function addCell($cell:BoardCell):void{
			if(this.cells.indexOf($cell)==-1){
				this.cells.push($cell);
				this.cellLength = this.cells.length;
			}else{
				throw new Event("");
			}
			this.checkJoint();
		}
		
		public function removeCell($cell:BoardCell):void{
			FuArray.removeValueFromArray(this.cells,$cell);
			this.cellLength = this.cells.length;
			this.checkJoint();
		}
		
		public function checkJoint():void{
			var $joint:IBoardJoint;
			switch(this.cellLength){
				case 2:
					$joint = CheckUtils.getJointBetweenCells(this.cells[0] as BoardCell,this.cells[1] as BoardCell);
					if($joint!=null){
						this.hasJointInCells = true;
						this.jointInCells = $joint;
						return;
					}
					break;
				case 3:
					$joint = CheckUtils.getJointAmongCells(this.cells[0] as BoardCell,this.cells[1] as BoardCell,this.cells[2] as BoardCell);
					if($joint!=null){
						this.hasJointInCells = true;
						this.jointInCells = $joint;
						return;
					}else{
						//do defualt;
					}
					break;
				default:
					break;
			}
			this.hasJointInCells = false;
			this.jointInCells = null;
		}
	}
}